/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:  
 * 
 **    Process the remote site monitor result info.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.BizServiceStatus;
import com.hp.snap.gr.model.DeployStatus;
import com.hp.snap.gr.model.SessionServerStatus;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.SNMPTrapHelper;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * Load remote deploy status from action output to process it. Get session server status and show in console.
 * 
 * There is no parameter configured in action.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua 
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class ProcessMonitorResultActionHandler extends AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(ProcessMonitorResultActionHandler.class);

    @SuppressWarnings("unchecked")
    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        DeployStatus deployStatus = (DeployStatus) actionPlanOutputs.get(Constants.REMOTE_DEPLOY_STATUS);
        String deploymentName = deployStatus.getDeployName();
        Map<String, Integer> ssActiveCounterRuleMap = Utils.getSSCounterForActive();
        Map<String, Integer> ssCountersWithRunningMap = deployStatus.filterSessionServerWithBizStatus(BizServiceStatus.Running);
        boolean siteRunningFlag = checkSStypeFailed(ssCountersWithRunningMap, ssActiveCounterRuleMap);
        deployStatus.setFailed(siteRunningFlag);
        String msg = "The " + deploymentName + " deployment site session server status:";
        String outStr = new StringBuffer().append(msg).append("\n").append(sessionServerStatusString(deployStatus)).toString();
        Constants.CONSOLE.info(outStr);
        if (deployStatus.isFailed()) {
            logger.info("Send messge to SNMP");
            SNMPTrapHelper.sendTrap(outStr);
        }

    }

    private boolean checkSStypeFailed(Map<String, Integer> ssCountersWithRunningMap, Map<String, Integer> ssActiveCounterRuleMap) {
        boolean ssRunning = true;
        for (String ssType : ssCountersWithRunningMap.keySet()) {
            int runningCounter = ssCountersWithRunningMap.get(ssType);
            int runningRule = ssActiveCounterRuleMap.get(ssType) == null ? 0 : ssActiveCounterRuleMap.get(ssType);
            ssRunning &= (runningCounter >= runningRule);
        }
        return !ssRunning;
    }

    private String sessionServerStatusString(DeployStatus deployStatus) {
        StringBuffer strBuf = new StringBuffer();
        Map<String, List<SessionServerStatus>> sessionServerStatusMap = deployStatus.getSessionServerStatusMap();
        for (String ssType : sessionServerStatusMap.keySet()) {
            List<SessionServerStatus> sessionServerStatusList = sessionServerStatusMap.get(ssType);
            for (SessionServerStatus obj : sessionServerStatusList) {
                strBuf.append("The session server:").append(obj.getName()).append(", status is:").append(obj.getProcessStatus()).append(", Biz service status is:").append(obj.getBizServiceStatus()).append("\n");
            }
        }
        return strBuf.toString();
    }

}
